﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;

namespace Delphi.Mimir.Data.Entities.Final.Mappings {
    public class TransactionMap : EntityTypeConfiguration<Transaction> {
        public TransactionMap() {
            /******************************************************************
            /* Table
            /*****************************************************************/
            ToTable("fin.pfm_transactions");

            /******************************************************************
            /* Primary Key
            /*****************************************************************/
            HasKey(a => a.TransactionId);

            /******************************************************************
            /* Index
            /*****************************************************************/
            // IX_TRN
            Property(a => a.AccountId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_TRN", 1) { IsUnique = false }));

            /******************************************************************
            /* Relationships
            /*****************************************************************/
            // 1..n Account
            HasRequired<Account>(a => a.Account)
                .WithMany()
                .HasForeignKey(a => a.AccountId);

            // 1..n Symbol
            HasOptional<Symbol>(a => a.Symbol)
                .WithMany()
                .HasForeignKey(a => a.SymbolId);

            // 1..n Currency
            HasRequired<Currency>(a => a.Currency)
                .WithMany()
                .HasForeignKey(a => a.CurrencyId)
                .WillCascadeOnDelete(false);

            /******************************************************************
            /* Properties
            /*****************************************************************/
            Property(a => a.TransactionId)
                .IsRequired()
                .HasColumnName("trn_id");

            Property(a => a.AccountId)
                .IsRequired()
                .HasColumnName("trn_act_id");

            Property(a => a.SymbolId)
                .IsOptional()
                .HasColumnName("trn_sym_id");

            Property(a => a.CurrencyId)
                .IsRequired()
                .HasColumnName("trn_cur_id");

            Property(a => a.OrderDate)
                .IsRequired()
                .HasColumnName("trn_order_ts");

            Property(a => a.ExecutionDate)
                .IsOptional()
                .HasColumnName("trn_exec_ts");

            Property(a => a.Units)
                .IsOptional()
                .HasPrecision(18, 6)
                .HasColumnName("trn_units");

            Property(a => a.Amount)
                .IsRequired()
                .HasPrecision(18, 6)
                .HasColumnName("trn_amount");

            Property(a => a.Fees)
                .IsRequired()
                .HasPrecision(18, 6)
                .HasColumnName("trn_fees");

            Property(a => a.AmountReferenceCurrency)
                .IsRequired()
                .HasPrecision(18, 6)
                .HasColumnName("trn_amount_ref_cur");

            Property(a => a.FeesReferenceCurrency)
                .IsRequired()
                .HasPrecision(18, 6)
                .HasColumnName("trn_fees_ref_cur");
        }
    }
}
